#include "stdafx.h"
#include "ThemeFun.h"


void PaintTBButton(LPNMCUSTOMDRAW pInfo)
{	
	TBBUTTON		tb = {0};
	UINT			nID = 0;
	UINT			nStyle = 0;
	int				iImage = 0;
	CImageList		IL((HIMAGELIST)::SendMessage(pInfo->hdr.hwndFrom, TB_GETIMAGELIST, 0, 0L));
	CImageList		DIL((HIMAGELIST)::SendMessage(pInfo->hdr.hwndFrom,TB_GETDISABLEDIMAGELIST, 0, 0L));
	CImageList*		pImgList = (pInfo->uItemState&CDIS_DISABLED) ? &DIL: &IL;
	CDCT<false>		dc(pInfo->hdc);
	CPoint			ptImage(pInfo->rc.left + 3,pInfo->rc.top + 3);
	CSize			imgSize;
	int				nIndex = (int)::SendMessage(pInfo->hdr.hwndFrom, TB_COMMANDTOINDEX, pInfo->dwItemSpec, 0);

	if (NULL == pImgList)
		return ;

	::SendMessage(pInfo->hdr.hwndFrom, TB_GETBUTTON, nIndex, (LPARAM)&tb);	
	tb.fsState ^= TBSTATE_ENABLED;
	nID		= tb.idCommand;
	nStyle	= MAKELONG(tb.fsStyle, tb.fsState);
	iImage	= tb.iBitmap;

	if (pInfo->uItemState & CDIS_CHECKED || pInfo->uItemState & CDIS_HOT)
	{
		COLORREF colorSel = art::GetXpHighlightColor();
		COLORREF colorBorder = GetSysColor(COLOR_HIGHLIGHT);
		dc.FillSolidRect( &(pInfo->rc),colorSel);
		dc.Draw3dRect(&(pInfo->rc), colorBorder, colorBorder);
		if (tb.fsStyle & TBSTYLE_DROPDOWN)
		{
			CPen pen;
			pen.CreatePen(PS_SOLID, 1, colorBorder);
			HPEN hOldPen = dc.SelectPen(pen);
			dc.MoveTo(pInfo->rc.right - 13, pInfo->rc.top);
			dc.LineTo(pInfo->rc.right - 13, pInfo->rc.bottom);
			dc.SelectPen(hOldPen);
		}
	}

	

	if(pInfo->uItemState&CDIS_HOT && !(pInfo->uItemState&CDIS_SELECTED))
	{
		if( *pImgList)
		{

			ptImage.x = pInfo->rc.left+4;
			ptImage.y = pInfo->rc.top+4;
			// draws the icon blended
			HICON hDrawIcon2 = pImgList->ExtractIcon(iImage);
			CBrush br;
			br.CreateSolidBrush( GetSysColor(COLOR_INACTIVECAPTION));
			dc.DrawState(ptImage, imgSize, hDrawIcon2, DSS_MONO,(HBRUSH)br);
			DestroyIcon(hDrawIcon2);

			ptImage.x = pInfo->rc.left+2;
			ptImage.y = pInfo->rc.top+2;
		}

	}

	if(*pImgList)
	{
		pImgList->Draw(pInfo->hdc, iImage, ptImage, ILD_TRANSPARENT);
		ATLTRACE("Image #%d drawn at POINT(%d, %d)\r\n", iImage, ptImage.x, ptImage.y);
		if (tb.fsStyle & TBSTYLE_DROPDOWN)
		{
			CFont font;
			font.CreatePointFont(80, _T("Marlett"));
			dc.SelectFont(font);
			pInfo->rc.left = pInfo->rc.right - 13;
			dc.SetBkMode(TRANSPARENT);
			dc.DrawText(_T("6"), 1, &pInfo->rc, DT_VCENTER|DT_VCENTER|DT_SINGLELINE);
		}
	}
}


LRESULT WINAPI Draw_ToolBar(int idCtrl, LPNMTBCUSTOMDRAW lptbCustomDraw)
{
	DWORD dwRet = 0;

	switch(lptbCustomDraw->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		dwRet = CDRF_NOTIFYITEMDRAW;
		break;
	case CDDS_POSTPAINT:
		//dwRet = OnPostPaint(idCtrl, lpNMCustomDraw);
		break;
	case CDDS_PREERASE:
		//dwRet = OnPreErase(idCtrl, lpNMCustomDraw);
		break;
	case CDDS_POSTERASE:
		//dwRet = OnPostErase(idCtrl, lpNMCustomDraw);
		break;
	case CDDS_ITEMPREPAINT:
		//dwRet = OnItemPrePaint(idCtrl, lpNMCustomDraw);

		PaintTBButton(&lptbCustomDraw->nmcd);

		ATLTRACE("(%d,%d,%d,%d)\n", lptbCustomDraw->nmcd.rc.left,
			lptbCustomDraw->nmcd.rc.top, lptbCustomDraw->nmcd.rc.right, lptbCustomDraw->nmcd.rc.bottom);
		// Tell Windows to paint the control itself.
		return CDRF_SKIPDEFAULT;
		break;
	case CDDS_ITEMPOSTPAINT:
		//dwRet = OnItemPostPaint(idCtrl, lpNMCustomDraw);
		break;
	case CDDS_ITEMPREERASE:
		//dwRet = OnItemPreErase(idCtrl, lpNMCustomDraw);
		break;
	case CDDS_ITEMPOSTERASE:
		//dwRet = OnItemPostErase(idCtrl, lpNMCustomDraw);
		break;

#if (_WIN32_IE >= 0x0400)
	case (CDDS_ITEMPREPAINT | CDDS_SUBITEM):
		//dwRet = OnSubItemPrePaint(idCtrl, lpNMCustomDraw);
		break;
#endif // (_WIN32_IE >= 0x0400)

	default:
		//SetMsgHandled(FALSE);
		break;
	}

	return dwRet;
}

